package com.koron.order.linedefect.base.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.linedefect.base.bean.po.DefectChange;
import com.koron.order.linedefect.base.bean.query.DefectChangeQuery;
import com.koron.order.linedefect.base.service.DefectChangeService;
import com.koron.order.risk.base.controller.LineRiskChangeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/lineDefect/orderChange/")
@RestController
@Api(tags = "线路缺陷工单变更")
public class DefectChangeController implements WorkflowController {


    @Autowired
    private DefectChangeService defectChangeService;


    public String getCurrDs(){
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "新增或保存缺陷变更单")
    @PostMapping("/saveOrUpdate")
    public Response saveOrUpdate(@RequestBody DefectChange defectChange){

        return ADOConnection.runTask(getCurrDs(), defectChangeService, "saveOrUpdate", Response.class, defectChange);
    }


    @ApiOperation(value = "分页查询缺陷变更单")
    @GetMapping("/page")
    public Response page(PageQuery pageQuery,  DefectChangeQuery defectChangeQuery){

        return ADOConnection.runTask(getCurrDs(), defectChangeService, "page", Response.class, pageQuery, defectChangeQuery);
    }


    @ApiOperation(value = "删除缺陷变更单")
    @GetMapping("/delete")
    public Response page(@RequestParam("id") String id){

        return ADOConnection.runTask(getCurrDs(), defectChangeService, "delete", Response.class, id);
    }

    @ApiOperation(value = "根据id获取缺陷变更单")
    @GetMapping("/selectOne")
    public Response getById(@RequestParam("id") String id){

        return ADOConnection.runTask(getCurrDs(), defectChangeService, "getById", Response.class, id);
    }


    @Override
    public Response<Object> updateState(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 撤回或者废弃
        String id = redirectQuery.getDataId();
        Integer status = null;
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 修改为草稿状态
            status = LineRiskChangeController.RiskChangeEnum.DRAFT.getStatus();
        } else {
            status = LineRiskChangeController.RiskChangeEnum.WAIT_REVIEW.getStatus();
        }
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), defectChangeService, "update", Response.class, id, status);
        return Response.success();
    }

    @Override
    public Response<Object> updateEnd(RedirectQuery redirectQuery) {
        String id = redirectQuery.getDataId();
        Integer status = LineRiskChangeController.RiskChangeEnum.ARCHIVED.getStatus();
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), defectChangeService, "update", Response.class, id, status);
        return Response.success();
    }
}
